
package com.frame.detection.service.impl;

import com.frame.detection.common.ImageDetector;
import com.frame.detection.service.ImageDetectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.IOException;


@Service
public class ImageDetectServiceImpl implements ImageDetectService {

    /**
     * logger of ImageDetectServiceImpl
     */
    private Logger logger = LoggerFactory.getLogger(ImageDetectServiceImpl.class);

    /**
     * image detector
     */
    @Autowired
    private ImageDetector imageDetector;

    /**
     * detect by url
     *
     * @param imageUrl image url
     */
    @Override
    public String detectByUrl_A(String imageUrl) {

        String detectResult = imageDetector.getDetectImageByUrl_A(imageUrl);
        return detectResult;
    }

    /**
     * detect by file
     *
     * @param imageFile imageFile
     */
    @Override
    public String detectByFile_A(MultipartFile imageFile) {

        try {
            BASE64Encoder base64Encoder = new BASE64Encoder();
            String base64EncoderImg = base64Encoder.encode(imageFile.getBytes());
            return imageDetector.getDetectImageByCode_A(base64EncoderImg);

        } catch (IOException e) {
            logger.error("imageFile convert failed");
        }
        return null;
    }

    @Override
    public String detectByUrl_G(String imageUrl) {
        String detectResult = imageDetector.getDetectImageByUrl_G(imageUrl);
        return detectResult;
    }

    @Override
    public String detectByFile_G(MultipartFile imageFile) {
        try {
            BASE64Encoder base64Encoder = new BASE64Encoder();
            String base64EncoderImg = base64Encoder.encode(imageFile.getBytes());
            return imageDetector.getDetectImageByCode_G(base64EncoderImg);

        } catch (IOException e) {
            logger.error("imageFile convert failed");
        }
        return null;
    }

    @Override
    public String detectByUrl_F(String imageUrl) {
        String detectResult = imageDetector.getDetectImageByUrl_F(imageUrl);
        return detectResult;
    }

    @Override
    public String detectByFile_F(MultipartFile imageFile) {
        try {
            BASE64Encoder base64Encoder = new BASE64Encoder();
            String base64EncoderImg = base64Encoder.encode(imageFile.getBytes());
            return imageDetector.getDetectImageByCode_F(base64EncoderImg);

        } catch (IOException e) {
            logger.error("imageFile convert failed");
        }
        return null;
    }



    @Override
    public String getOriginalImageByUrl(String imageUrl) {
        String detectResult = imageDetector.getDetectImageByUrl_h(imageUrl);
        return detectResult;
    }

    @Override
    public String getOriginalImageByFile(MultipartFile imageFile) {
        try {
            BASE64Encoder base64Encoder = new BASE64Encoder();
            String base64EncoderImg = base64Encoder.encode(imageFile.getBytes());
            return imageDetector.getDetectImageByCode_h(base64EncoderImg);

        } catch (IOException e) {
            logger.error("imageFile convert failed");
        }
        return null;
    }

}